import Vue from "vue"
import MessageBox from "./MessageBox"

export let messageBox = (function() {
    // 默认配置
    return function(opt) {
        // 默认参数
        let defaults = {
            title: "",
            content: "",
            cancel: "",
			ok: "",
			handleCancel: null,
			handleOk: null
        };
        let Components = Vue.extend(MessageBox)
        for (const key in opt) {
            defaults[key] = opt[key]
        }
        
        const myVm = new Components({
            el: document.createElement("div"),
            data() {
                return{
                    title: defaults.title,
                    content: defaults.content,
                    cancel: defaults.cancel,
                    ok: defaults.ok,
                }
            },
            methods: {
                handleCancel() {
                    defaults.handleCancel && defaults.handleCancel.call(this)
                    document.body.removeChild(myVm.$el)
                },
                handleOk() {
                    defaults.handleOk && defaults.handleOk.call(this)
                    document.body.removeChild(myVm.$el)
                }
            }
        })
        document.body.appendChild(myVm.$el)
    };
})();